import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_example/get_put_find/controller/my_product_controller.dart';
import 'package:getx_example/get_put_find/page/my_product_detail_page.dart';

class MyProductPage extends StatelessWidget {
  //路由地址
  static const String route = "/MyProductPage";

  const MyProductPage({super.key});

  @override
  Widget build(BuildContext context) {
    final MyProductController productController = Get.put<MyProductController>(MyProductController());

    return Scaffold(
        appBar: AppBar(
          title: const Text("路由跳转"),
          backgroundColor: context.theme.primaryColor,
        ),
        body: SingleChildScrollView(
            child: Column(children: [
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("当验证 Get.off的时候点击此处, 然后返回, 看下此页面是否 在堆栈中"),
              subtitle: const Text('Get.off(const MyProductDetailPage());'),
              leading: const Icon(Icons.home),
              trailing: const Icon(Icons.arrow_forward_ios),
              onTap: () {
                Get.off(const MyProductDetailPage());
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("当验证 Get.off的时候点击此处, 然后返回, 已经无法返回了, 因为没有堆栈信息了, 慎用"),
              subtitle: const Text('Get.offAll(const MyProductDetailPage());'),
              leading: const Icon(Icons.home),
              trailing: const Icon(Icons.arrow_forward_ios),
              onTap: () {
                Get.offAll(const MyProductDetailPage());
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("当验证 Get.put-find controller"),
              subtitle: GetX<MyProductController>(
                init: productController,
                builder: (MyProductController controller) {
                  return Column(
                    children: controller.myProductList
                        .map((element) => Text("data: ${element.name} ${element.price} ${element.description} x ${controller.count}"))
                        .toList(),
                  );
                },
              ),
              leading: const Icon(Icons.home),
              trailing: const Icon(Icons.arrow_forward_ios),
              onTap: () {
                Get.to(const MyProductDetailPage());
              },
            ),
          ),
        ])));
  }
}
